package com.snack.shop.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.snack.shop.common.Result;
import com.snack.shop.entity.Order;
import com.snack.shop.service.RefundService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * 退款管理控制器（商家端）
 * 
 * @author 阿狄
 * @date 2025-11-13
 */
@Tag(name = "退款管理")
@RestController
@RequestMapping("/merchant/refund")
@RequiredArgsConstructor
@SaCheckRole("merchant")
public class RefundController {

    private final RefundService refundService;

    @Operation(summary = "获取待审核退款列表")
    @GetMapping("/pending")
    public Result<Page<Order>> getPendingRefunds(
        @Parameter(description = "当前页") @RequestParam(defaultValue = "1") Integer current,
        @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Integer size
    ) {
        Page<Order> page = refundService.getPendingRefunds(current, size);
        return Result.success(page);
    }

    @Operation(summary = "获取已处理退款记录")
    @GetMapping("/processed")
    public Result<Page<Order>> getProcessedRefunds(
        @Parameter(description = "当前页") @RequestParam(defaultValue = "1") Integer current,
        @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Integer size
    ) {
        Page<Order> page = refundService.getProcessedRefunds(current, size);
        return Result.success(page);
    }

    @Operation(summary = "审核退款")
    @PostMapping("/approve")
    public Result<RefundApprovalResult> approveRefund(@RequestBody RefundApprovalRequest request) {
        refundService.approveRefund(
            request.getOrderId(),
            request.getApproved(),
            request.getCompensationRate(),
            request.getRejectReason()
        );

        RefundApprovalResult result = new RefundApprovalResult();
        if (request.getApproved()) {
            result.setMessage("退款处理成功");
            if (request.getCompensationRate() != null) {
                BigDecimal[] amounts = refundService.calculateRefundAmount(
                    new Order() {{ setActualAmount(request.getOrderAmount()); }}, 
                    request.getCompensationRate()
                );
                result.setRefundAmount(amounts[0]);
                result.setCompensationAmount(amounts[1]);
            }
        } else {
            result.setMessage("退款已拒绝");
        }

        return Result.success(result);
    }

    /**
     * 退款审核请求参数
     */
    public static class RefundApprovalRequest {
        private Long orderId;
        private Boolean approved;
        private BigDecimal compensationRate;
        private String rejectReason;
        private BigDecimal orderAmount; // 用于计算退款金额展示

        public Long getOrderId() {
            return orderId;
        }

        public void setOrderId(Long orderId) {
            this.orderId = orderId;
        }

        public Boolean getApproved() {
            return approved;
        }

        public void setApproved(Boolean approved) {
            this.approved = approved;
        }

        public BigDecimal getCompensationRate() {
            return compensationRate;
        }

        public void setCompensationRate(BigDecimal compensationRate) {
            this.compensationRate = compensationRate;
        }

        public String getRejectReason() {
            return rejectReason;
        }

        public void setRejectReason(String rejectReason) {
            this.rejectReason = rejectReason;
        }

        public BigDecimal getOrderAmount() {
            return orderAmount;
        }

        public void setOrderAmount(BigDecimal orderAmount) {
            this.orderAmount = orderAmount;
        }
    }

    /**
     * 退款审核结果
     */
    public static class RefundApprovalResult {
        private String message;
        private BigDecimal refundAmount;
        private BigDecimal compensationAmount;

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public BigDecimal getRefundAmount() {
            return refundAmount;
        }

        public void setRefundAmount(BigDecimal refundAmount) {
            this.refundAmount = refundAmount;
        }

        public BigDecimal getCompensationAmount() {
            return compensationAmount;
        }

        public void setCompensationAmount(BigDecimal compensationAmount) {
            this.compensationAmount = compensationAmount;
        }
    }
}
